package com.lucky.auth_boot.config;


import com.lucky.auth_boot.security.filter.JwtAuthenticationFilter;
import com.lucky.auth_boot.security.UserServices;
import com.lucky.auth_boot.security.handler.AuthenticationExceptionHandler;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableMethodSecurity
@Configuration
public class WebSecurityConfig {
    @Resource
    private AuthenticationExceptionHandler authenticationExceptionHandler;
    @Resource
    UserServices userServices;
    @Resource
    JwtAuthenticationFilter jwtAuthenticationFilter;

    //    白名单
    String[] urls = {
            "/admin/login",
            "/admin/code",
            "/user/register",
            "/druid/**",
            "/domain/getEmpowerUrl",
            "/**/*.js",
            "/**/*.css",
            "/swagger-resources",
            "/v2/api-docs"
    };
    @Bean
    public WebSecurityCustomizer securityCustomizer() {
        return (web) -> web.ignoring().requestMatchers("/static/**");
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        //        关csrf保护
        http.csrf(AbstractHttpConfigurer::disable);
        // 前后端分离是无状态的，不需要session了，直接禁用。
        http.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));

//        请求授权
        http.authorizeHttpRequests((requests) -> {
            requests.requestMatchers("/admin/login","/admin/code","/user/register","/druid/**","/domain/getEmpowerUrl").permitAll() //1 首页所有人允许
//                    .anyRequest().permitAll(); //2 剩下任意请求都需要 认证（登陆）
                    .anyRequest().authenticated(); //2 剩下任意请求都需要 认证（登陆）
        });

        http.exceptionHandling(exceptions -> exceptions.authenticationEntryPoint(authenticationExceptionHandler));

        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    AuthenticationManager authenticationManager(){
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userServices);
        return new ProviderManager(daoAuthenticationProvider);
    }
}